<?php

namespace App\Http\Controllers\Shop;

use App\Models\Shop;
use App\Models\ShopRecord;
use App\Models\ShopVisitor;

class ServiceController extends BaseController
{
    /**
     * 访客统计
     * @return \Illuminate\Http\JsonResponse
     */
    public function stat()
    {
        $shop_id = $this->request->input('shop_id');
        $url = $this->request->input('url');
        $visit_date = date('Ymd');

        ShopVisitor::insert([
            'uid'=>$this->uid,
            'username'=>$this->username,
            'shop_id'=>$shop_id,
            'url'=>$url,
            'ip'=>$this->request->getClientIp(),
            'visit_date'=>date('Ymd'),
            'visit_at'=>time()
        ]);

        Shop::where('shop_id', $shop_id)->increment('views');

        if (ShopRecord::where(['shop_id'=>$shop_id, 'datestamp'=>date('Ymd')])->exists()){
            ShopRecord::where(['shop_id'=>$shop_id, 'datestamp'=>date('Ymd')])->increment('visitors');
        }else {
            ShopRecord::insert([
                'shop_id'=>$shop_id,
                'datestamp'=>date('Ymd'),
                'views'=>1
            ]);
        }
        return ajaxReturn(['shop_id'=>$shop_id]);
    }
}
